package com.ciaojian.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ciaojian.core.model.Coupon;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;

/**
 * @author Atlakyn
 */
@Mapper
public interface CouponMapper extends BaseMapper<Coupon> {


    /**
     * 获取券码的实际过期时间
     *
     * @param couponId 券码ID
     * @return 实际过期时间
     */
    @Select("SELECT IF(date_type = 0,end_time, DATE_ADD(CURDATE(),INTERVAL same_day DAY )) FROM d_coupon WHERE id = #{couponId} ")
    Date getCouponExpiredTimeById(@Param("couponId") Integer couponId);

    /**
     * 券领取数量+1
     *
     * @param id 券类型
     * @return 增加结果
     */
    @Update("UPDATE d_coupon SET get_num = get_num +1 WHERE id =#{id} ")
    Boolean addGetNumById(@Param("id") Integer id);

    /**
     * 券领取数量+1
     *
     * @param ids 券类型
     * @return 增加结果
     */
    Boolean addGetNumByIds(@Param("ids") String[] ids);

    /**
     * 操作库存
     * 使用CAS机制
     *
     * @param couponId /
     * @return /
     */
    Boolean operateStock(@Param("couponId") Integer couponId);
}
